<?php
class Admin_LoginController extends Zend_Controller_Action {
	function init(){
		parent::init();
		$this->registry=Zend_Registry::getInstance();
      	$this->view=$this->registry['view'];
     	$this->view->baseUrl=BLOG_ROOT_URL;
     	$this->view->styledir=$this->view->baseUrl."/public/styles/admin";
     	//改变模板文件位置
		$this->view->setScriptPath(APP_DIR.'/views/scripts/admin/index/');
	}
	public function indexAction(){
		//验证登陆
		$this->view->title = "后台登陆页面";
		echo $this->view->render("login.phtml");
	}
	public function checkloginAction(){
		if ($this->_request->isPost()):
		$filter = new Zend_Filter_StripTags();
		$user = $filter->filter($this->_request->getPost('user'));
		$pwd = md5($filter->filter($this->_request->getPost('pwd')));
		if (empty($user) || empty($pwd)) {
			php_alert("用户名密码不能为空");
			$this->_redirect('admin/login');
			exit;
    	}
		$db = Zend_Registry::get('db');
		$authAdapter = new Zend_Auth_Adapter_DbTable($db);
		$authAdapter->setTableName('zf_user');
		$authAdapter->setIdentityColumn('u_name');
		$authAdapter->setCredentialColumn('u_pwd');
		$authAdapter->setIdentity($user);
		$authAdapter->setCredential($pwd);

		$auth = Zend_Auth::getInstance();
		$result = $auth->authenticate($authAdapter);
    	if($result->isValid()){
			$data = $authAdapter->getResultRowObject();
			//写入session
			$auth->getStorage()->write($data);
			$this->_redirect('admin');
    	}else{
    		php_alert("用户名或密码错误");
			$this->_redirect('admin/login');
			exit;
    	}
    	endif;
		return true;
	}
	public function logoutAction(){
		//清除session
		if(Zend_Auth::getInstance()->clearIdentity()){
			php_alert('退出成功');
		}
		//echo $this->view->render('login.phtml');
		$this->_redirect('http://www.ph-blog.net');
	}
	
	public function noRouteAction(){
		$this->_redirect('admin');
	}
}
?>